package com.conversor;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

import com.core.enumerators.ComprimentoType;
import com.core.enumerators.MassaType;
import com.core.enumerators.TemperaturaType;

public class ActivityBase extends Activity {	
	
	protected String[] tiposTemperaturaGrid = {TemperaturaType.Celcius.toString(),TemperaturaType.Fahrenheit.toString(),TemperaturaType.Kelvin.toString()};
	protected String[] tiposMassaGrid = {MassaType.Miligrama.toString(),MassaType.Grama.toString(),MassaType.Quilograma.toString(), MassaType.Libra.toString(),MassaType.Onca.toString()};
	protected String[] tiposComprimentoGrid = {ComprimentoType.Milimetro.toString(),ComprimentoType.Centimetro.toString(),ComprimentoType.Metro.toString(),ComprimentoType.Quilometro.toString(), ComprimentoType.Jarda.toString(),ComprimentoType.Pe.toString(),ComprimentoType.Polegada.toString()};
	
	protected TemperaturaType tipoTempSelecionadoEnum;
	protected MassaType tipoMassaSelecionadoEnum;
	protected ComprimentoType tipoCompSelecionadoEnum;
	
	protected Spinner spinner;	
	protected TableLayout tableLayout;
	protected String tipoSelecionado;
	
	protected int QuantidadeTipoTemperatura = 3;
	protected int QuantidadeTipoMassa = 5;
	protected int QuantidadeTipoComprimento = 7;
	
	private Spinner getSpinnerById(int spinnerID){
		return (Spinner)findViewById(spinnerID);
	}
	
	protected void InitializeTableLayoutById(int tableLayoutId){
		tableLayout = (TableLayout) findViewById(tableLayoutId);
		tableLayout.removeAllViews();		
	}
	
	protected void FillSpinner(int spinnerID, String[] tipos){
		spinner = getSpinnerById(spinnerID);
		
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tipos);
	        
	    ArrayAdapter<String> spinnerArrayAdapter = arrayAdapter;
	    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
	    spinner.setAdapter(spinnerArrayAdapter);	       
	}
	
	protected void FillTableRow(Context context, String valueConverted, String tipo, int position){
				
		// Create a TableRow and give it an ID
        TableRow tr = new TableRow(context);
        tr.setId(100+position);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        
     // Create a TextView to house the value of the after-tax income
        TextView valueTV = new TextView(context);
        valueTV.setId(position);
        valueTV.setText(ValidateMaxLengh(valueConverted));  
        valueTV.setTextSize(20);
        valueTV.setTextColor(Color.WHITE);	  
        valueTV.setWidth(127);
        valueTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(valueTV);
        
     // Create a TextView to house the name of the province
        TextView labelTV = new TextView(context);
        labelTV.setId(200+position);
        labelTV.setText(tipo);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setTextSize(20);
        labelTV.setWidth(125);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);
        
     // Add the TableRow to the TableLayout
        tableLayout.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.MATCH_PARENT));
       	
	}
	
	private String ValidateMaxLengh(String valueConverted){
		int y;  
        String first;
        String second;  
        first = valueConverted;  
        y = first.indexOf(".");  
        second = first.substring(y, first.length());  
        if (second.length() > 10) {  
        	second = second.substring(0, 10);		                
        }  
        first = first.substring(0, y);  
        valueConverted = (first + second); 
        while (valueConverted.endsWith("0")){
        	valueConverted = (valueConverted.substring(0, valueConverted.length() - 1));
        }
        
        if(valueConverted.endsWith(".")){
        	valueConverted = (valueConverted.substring(0, valueConverted.length() - 1));
        }        
        return valueConverted;        
	}
		
}
